package com.wbao.dianniu.adapter;

import android.graphics.Bitmap;
import android.util.Log;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.Utils;

/**
 * Created by yejianjun on 2018/6/20.
 */

public class AdViewTarget extends ViewTarget<ImageView, Bitmap> {

    private ImageView imageView;

    public AdViewTarget(ImageView view) {
        super(view);
        this.imageView = view;
    }

    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        TypedValue outValue = new TypedValue();
        imageView.getResources().getValue(R.dimen.width_height_rate, outValue, true);
        float value = outValue.getFloat();
        int width = imageView.getWidth();
        int pad_left = imageView.getPaddingLeft();
        int pad_right = imageView.getPaddingRight();
        int height = (int) ((width - pad_left - pad_right) / value);
        Log.e("TAG", "width: " + width + " heigh:" + height);
        ViewGroup.LayoutParams lp = imageView.getLayoutParams();
        lp.width = imageView.getWidth();
        lp.height = height;
        imageView.setPadding(pad_left, 0, pad_right, 0);
        imageView.setLayoutParams(lp);
        imageView.setImageBitmap(resource);
    }
}
